package com.sanpu.kxe.business.main.presenter;

import com.sanpu.kxe.business.main.bean.EventCenterOutput;
import com.sanpu.kxe.business.main.contract.EventCenterContract;
import com.sanpu.kxe.business.main.model.MainModel;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：活动中心
 * Created by 薛勇军 on 2018/7/12.
 */

public class EventCenterPresenter implements EventCenterContract.Presenter {

    private EventCenterContract.View mView;

    private CompositeSubscription mSubscription;

    public EventCenterPresenter(EventCenterContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getActInfList() {
        mView.showLoadingDialog(true);
        Subscription subscription = MainModel.getInstance().getActInfList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<EventCenterOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<EventCenterOutput> eventCenterOutputs) {
                        mView.showViewData(eventCenterOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mView != null)
            mView = null;
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
    }
}
